package com.ly.designpatten.single;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Classname LazySingleInnerClass
 * @Description TODO
 * @Date 2020/10/5 14:43
 * @Author 冷心影翼
 */
public class LazySingleInnerClass {

	private LazySingleInnerClass() {
		System.out.println("initLazySingleInnerClass");
	}

	public static class Inner {
		private static final LazySingleInnerClass LAZY_SINGLE_INNER_CLASS = new LazySingleInnerClass();
	}

	public static LazySingleInnerClass getInstance() {
		return Inner.LAZY_SINGLE_INNER_CLASS;
	}
}

class TestLazySingleInnerClass {
	public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
		for(int i=0;i<10;i++) {
			new Thread(()->{LazySingleInnerClass.getInstance();}).start();
		}

		Constructor<LazySingleInnerClass> constructor = LazySingleInnerClass.class.getDeclaredConstructor(null);

		constructor.setAccessible(true);
		LazySingleInnerClass lazySingleInnerClass = constructor.newInstance();
		LazySingleInnerClass lazySingleInnerClass1 = constructor.newInstance();
	}
}

